home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / libkb100.zip / LIBKB-1.00 / SAMPLES / KBTST.C < prev    next >
C/C++ Source or Header  |  1996-07-23  |  11KB  |  520 lines

  1. /* kbtst.c -- test program for the libkb keyboard library
  2.  * Copyright (C) 1995, 1996 Markus F.X.J. Oberhumer
  3.  * For conditions of distribution and use, see copyright notice in kb.h 
  4.  */
  5.  
  6. /* note: this file has become somewhat messy, but I don't
  7.  * want to split it into different programs because it is really a pain to
  8.  * keep various Makefiles for various variants of Make up to date.
  9.  */
  10.  
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <signal.h>
  17. #if defined(__KB_LINUX) || defined(__DJGPP__)
  18. #  include <unistd.h>
  19. #endif
  20.  
  21. #include <kb.h>
  22. #include "intro.h"    
  23. #include "myconio.h"
  24.  
  25.  
  26. /*************************************************************************
  27. // signal handler
  28. // (this may not get called if you use the ncurses library)
  29. **************************************************************************/
  30.  
  31. static void my_signal_handler(int signum)
  32. {
  33.     /* file I/O is somewhat dangerous within a signal handler ... */
  34.     fflush(stderr);
  35.     if (signum == SIGINT)
  36.         fprintf(stderr,"SIGINT received.\n");
  37. #if defined(SIGBREAK)
  38.     else if (signum == SIGBREAK)
  39.         fprintf(stderr,"SIGBREAK received.\n");
  40. #endif
  41.     else 
  42.         fprintf(stderr,"SIGNAL %d received.\n",signum);
  43.     fflush(stderr);
  44.  
  45.     exit(128 + (signum & 0x7f));
  46. }
  47.  
  48.  
  49. /***********************************************************************
  50. // 
  51. ************************************************************************/
  52.  
  53. static int mode;
  54.  
  55. static int KBHIT(void)
  56. {
  57.     kb_update();
  58.  
  59.     if (mode <= 2)
  60.         return kb_kbhit();
  61.     else if (mode == 3)
  62.         return kb_os_kbhit();
  63.     else if (mode == 4)
  64.         return kb_bios_kbhit();
  65. #if defined(__KB_MSDOS)
  66.     else if (mode == 5)
  67.         return kbhit();
  68. #endif
  69.     else
  70.         return 0;
  71. }
  72.  
  73.  
  74. static unsigned long GETKEY(void)
  75. {
  76.     kb_update();
  77.  
  78.     if (mode == 1)
  79.         return kb_inkey();
  80.     else if (mode == 2)
  81.         return kb_getkey();
  82.     else if (mode == 3)
  83.         return kb_os_getkey();
  84.     else if (mode == 4)
  85.         return kb_bios_getkey();
  86. #if defined(__KB_MSDOS)
  87.     else if (mode == 5)
  88.         return getch();
  89. #endif
  90.     else
  91.         return 0;
  92. }
  93.  
  94.  
  95. static int q_pressed(void)
  96. {
  97.     kb_update();
  98.  
  99.     if (kb_key(KB_SCAN_Q))
  100.     {
  101.         if ((kb_key(KB_SCAN_LCONTROL) || kb_key(KB_SCAN_RCONTROL)) &&
  102.             (kb_key(KB_SCAN_LSHIFT) || kb_key(KB_SCAN_RSHIFT)))
  103.             return 1;
  104.         if (kb_key(KB_SCAN_ALTGR))
  105.             return 1;
  106.     }
  107.     return 0;
  108. }
  109.  
  110.  
  111. /***********************************************************************
  112. // cheating
  113. ************************************************************************/
  114.  
  115. static int cheat(unsigned key, const char *cheat_str, int *cheat_pos)
  116. {
  117.     if (key == 0)
  118.         return 0;
  119.     if (key == (unsigned char) cheat_str[*cheat_pos])
  120.     {
  121.         (*cheat_pos)++;
  122.         if (cheat_str[*cheat_pos] == 0)
  123.         {
  124.             *cheat_pos = 0;
  125.             return 1;            /* cheat activated */
  126.         }
  127.     }
  128.     else
  129.         *cheat_pos = 0;
  130.     return 0;
  131. }
  132.  
  133.  
  134. /***********************************************************************
  135. // conio interface
  136. ************************************************************************/
  137.  
  138. #define cputtext(x,y,text)        ((void)(gotoxy(x,y), cputs(text)))
  139. #define cputtexta(x,y,text,n)    ((void)(gotoxy(x,y), cputsa(text,n)))
  140.  
  141. static int conio_interface(unsigned long kflags)
  142. {
  143.     int i, x, y;
  144.     unsigned k;
  145.     char m[255];
  146.     int do_beep = 0;
  147.     unsigned color = LIGHTGRAY;
  148.  
  149. #if defined(__TURBOC__) || defined(__GO32__)
  150.     struct text_info ti;
  151.     gettextinfo(&ti);
  152.     if (ti.screenwidth < 80 || ti.screenheight < 25)
  153.         return 0;
  154. #endif
  155.  
  156.     if (kb_install(kflags) != 0)
  157.         return -1;
  158.  
  159. #if defined(cinit)
  160.     if (cinit() != 0)
  161.         return -2;
  162. #endif
  163.     
  164. #if defined(__NCURSES_H)
  165.     /* init colors */
  166.     for (i = 0; i < COLORS && i < COLOR_PAIRS; i++)
  167.         init_pair(i, i, COLOR_BLACK);
  168. #endif
  169.     _setcursortype(_NOCURSOR);
  170.     clrscr();
  171.  
  172. /* write intro */
  173.     x = 1; y = 1;
  174.     textcolor(YELLOW);
  175.     cputtext(x,y++,_kb_intro_text(m));
  176.     cputtext(x,y++,"libkb -- a free, advanced and portable low-level keyboard library");
  177.  
  178.     textcolor(color);
  179.     y++;
  180.     cputtext(x,y++,"Please send your bug reports, fixes, enhancements or suggestions to:\n");
  181.     cputtext(x,y++,"Markus F.X.J. Oberhumer <markus.oberhumer@jk.uni-linz.ac.at>\n");
  182.     y++;
  183.     cputtext(x,y++,KB_INSTALLED_MSG);
  184.     cputtext(x,y++,"Hit any key to test, Ctrl-Shift-Q or AltGr-Q to quit");
  185.  
  186. /* write constant text */
  187.     y += 2;
  188.     x = 12;
  189.     cputtext(x,y++,"          1         2         3         ");
  190.     cputtext(x,y++,"0123456789012345678901234567890123456789");
  191.     y += 2;
  192.     cputtext(x,y++,"4         5         6         7         ");
  193.     cputtext(x,y++,"0123456789012345678901234567890123456789");
  194.     cputtext(x+46,y-1,"last key pressed");
  195.     y += 2;
  196.     cputtext(x,y++,"                    1         1");
  197.     cputtext(x,y++,"8         9         0         1         ");
  198.     cputtext(x,y++,"0123456789012345678901234567890123456789");
  199.     cputtext(x+46,y-1,"shift flags");
  200.  
  201.  
  202. /* let's begin */
  203.     while (!q_pressed())
  204.     {
  205. #if defined(__NCURSES_H)
  206.         chtype mm[80+1];
  207.         const chtype kk = ACS_DIAMOND;
  208. #elif defined(__KB_LINUX)
  209.         char *mm = m;
  210.         const char kk = 'o';
  211. #else
  212.         char *mm = m;
  213.         const char kk = 0x1e;
  214. #endif
  215.         unsigned code;
  216.  
  217.     /* clear information bits if Esc pressed */
  218.         if (kb_key(KB_SCAN_ESC))
  219.             kb_shift_off(KB_SHIFT_OVERFLOW | KB_SHIFT_UNKNOWN);
  220.  
  221.     /* print keys */
  222.         textcolor(GREEN);
  223.         mm[40] = 0;
  224.         for (i = 0; i < 40; i++)
  225.             mm[i] = kb_key(i) ? kk : ' ';
  226.         cputtexta(x,y-9,mm,40);
  227.         for ( ; i < 80; i++)
  228.             mm[i-40] = kb_key(i) ? kk : ' ';
  229.         cputtexta(x,y-5,mm,40);
  230.         for ( ; i < 120; i++)
  231.             mm[i-80] = kb_key(i) ? kk : ' ';
  232.         cputtexta(x,y,mm,40);
  233.  
  234.     /* print shift flags */
  235.         textcolor(LIGHTRED);
  236.         k = kb_shift() & KB_SHIFT_ANY;
  237.         for (i = 0; i < 12; i++, k >>= 1)
  238.             mm[i] = k & 1 ? kk : ' ';
  239.         mm[i] = 0;
  240.         cputtexta(x+46,y,mm,i);
  241.         textcolor(LIGHTMAGENTA);
  242.         k = kb_shift();
  243.         i = 0;
  244.         mm[i++] = ' '; mm[i++] = ' ';
  245.         mm[i++] = k & KB_SHIFT_OVERFLOW ? 'o' : ' ';
  246.         mm[i++] = k & KB_SHIFT_UNKNOWN ?  'u' : ' ';
  247.         mm[i] = 0;
  248.         cputsa(mm,i);
  249.  
  250.     /* print name of last key pressed */
  251.         textcolor(CYAN);
  252.         k = kb_last_key();
  253.         strcpy(m,kb_keyname(k));
  254.         if (m[0] == 0) {
  255.             textcolor(RED);
  256.             if (do_beep)
  257.                 cbeep(), do_beep = 0;    /* beep if no name found */
  258.         } else
  259.             do_beep = 1;
  260.         for (i = strlen(m); i < 20; i++)
  261.             m[i] = ' ';
  262.         m[20] = 0;
  263.         code = kb_keycode(k);
  264.         if (code >= 32 && code < 127)
  265.             i = 16, m[i++] = '\'', m[i++] = code, m[i++] ='\'';
  266.         cputtext(x+46,y-5,m);
  267.         sprintf(m,"%3d  0x%04x  0x%04x", k & 0x7f, k, code);
  268.         cputtext(x+46,y-4,m);
  269.  
  270.     /* print scan codes of all pressed keys */
  271.         textcolor(color);
  272.         sprintf(m,"%2d keys now pressed: ", kb_keys_pressed());
  273.         for (i = 0; i < 128; i++)
  274.             if (kb_key(i))
  275.                 sprintf(m+strlen(m), "%d ", i);
  276.         for (i = strlen(m); i < 80; i++)
  277.             m[i] = ' ';
  278.         m[80] = 0;
  279.         cputtext(1,y+2,m);
  280.  
  281.     /* cheat section */
  282.         if (kb_kbhit())
  283.         {
  284.             static char cheat_str[] = "cheat";
  285.             static char mfx_str[] = "mfx";
  286.             static int cheat_pos = 0, mfx_pos = 0;
  287.  
  288.             k = kb_keypress();            /* get key (scancode) */
  289.             k &= 0x7f;                    /* ignore shift state */
  290.             k = kb_keycode(k);            /* convert to ASCII code */
  291.             if (k & 0xff00)                /* not a 'simple' key */
  292.                 k = 0;
  293.  
  294.             if (cheat(k,cheat_str,&cheat_pos))
  295.                 color = (color != LIGHTGRAY) ? LIGHTGRAY : LIGHTBLUE;
  296.             if (cheat(k,mfx_str,&mfx_pos))
  297.                 color = (color != LIGHTGRAY) ? LIGHTGRAY : LIGHTGREEN;
  298.         }
  299.     }
  300.  
  301. /* restore terminal */
  302.     gotoxy(1,25);
  303.     _setcursortype(_NORMALCURSOR);
  304. #if defined(__TURBOC__) || defined(__GO32__)
  305.     textattr(ti.attribute);
  306. #else
  307.     textcolor(LIGHTGRAY);
  308. #endif
  309.     cexit();
  310.  
  311.     return 0;
  312. }
  313.  
  314.  
  315. /***********************************************************************
  316. // show a keypress
  317. ************************************************************************/
  318.  
  319. static void show_keys(unsigned long c, unsigned long c2)
  320. {
  321.     int a = (int)c & 0xff;
  322.  
  323.     printf("num=%3d  st=%04x  ", kb_keys_pressed(), kb_shift() & KB_SHIFT_ANY);
  324.  
  325.     if (a >= 32 && a < 127)
  326.         printf("'%c'", (char) a);
  327.     else
  328.         printf("   ");
  329.  
  330.     printf("  key=%08lx", c);
  331.  
  332.     /* Extended character ? (from getch()) */
  333.     if (c2 != 0)
  334.         printf(" %08lx", c2);
  335.  
  336.     printf("  ");
  337.     _kb_port_debug();
  338.     printf("\n");
  339.     fflush(stdout);
  340. }
  341.  
  342.  
  343. /***********************************************************************
  344. // run a simple benchmark
  345. ************************************************************************/
  346.  
  347. static void benchmarck(void)
  348. {
  349.     clock_t t1, t2;
  350.     long i, n, k;
  351.  
  352.     printf("Running speed test (this can tak